import os

from win32com.client import DispatchEx, Dispatch

'''
https://open.wps.cn/docs/client/wpsLoad
https://learn.microsoft.com/zh-cn/office/vba/api/overview/
'''


def verify_param(param):
    if type(param) is not str:
        raise TypeError('参数不是字符串')
    else:
        if len(param) == 0 or param.isspace():
            raise Exception('参数不能为空字符串')


WPS_ = 'Kwps.application'
ET_ = 'Ket.application'
WPP_ = 'Kwpp.application'

WPS = Dispatch(WPS_)
ET = Dispatch(ET_)
WPP = Dispatch(WPP_)


def word_to_pdf(in_path, out_path):
    try:
        verify_param(in_path)
        verify_param(out_path)
        # Application = DispatchEx(WPS)
        Application = WPS
    except Exception as e:
        raise e
    if Application is not None:
        try:
            print(str(Application.Name) + ' ' + str(Application.Version))
            Application.Visible = False
            Document = Application.Documents.Open(in_path, 'false', 'true')
            Document = Document.ExportAsFixedFormat(out_path, '17')
            Document.Close()
        except Exception as e:
            Application.Quit()
            raise e
        finally:
            # Application.Quit()
            pass


def word_merge(in_paths, out_path):
    try:
        if type(in_paths) is not list:
            raise TypeError('参数不是字符串')
        verify_param(out_path)
        # Application = DispatchEx(WPS)
        Application = WPS
    except Exception as e:
        raise e
    if Application is not None:
        try:
            Document = Application.Documents.Add()
            for path in in_paths:
                Document_ = Application.Documents.Open(path)
                Application.Selection.WholeStory()
                Application.Selection.Copy()
                Application.Windows.Item(1).Activate()
                Application.Selection.TypeParagraph()
                Application.Selection.Paste()
                Document_.Close()
            if Document is not None:
                Document.SaveAs2(out_path)
                Document.Close()
        except Exception as e:
            Application.Quit()
            raise e
        finally:
            # Application.Quit()
            pass


def execl_to_pdf(in_path, out_path):
    try:
        verify_param(in_path)
        verify_param(out_path)
        # Application = DispatchEx(ET)
        Application = ET
    except Exception as e:
        raise e
    if Application is not None:
        try:
            print(str(Application.Name) + ' ' + str(Application.Version))
            Application.Visible = False
            Workbook = Application.Workbooks.Open(in_path)
            Workbook.ExportAsFixedFormat('0', out_path)
            Workbook.Close()
        except Exception as e:
            Application.Quit()
            raise e
        finally:
            # Application.Quit()
            pass


def presentation_to_pdf(in_path, out_path):
    try:
        verify_param(in_path)
        verify_param(out_path)
        # Application = DispatchEx(WPP)
        Application = WPP
    except Exception as e:
        raise e
    if Application is not None:
        try:
            print(str(Application.Name) + ' ' + str(Application.Version))
            Presentation = Application.Presentations.Open(in_path)
            Presentation.SaveAs(out_path, '32')
            Presentation.Close()
        except Exception as e:
            Application.Quit()
            raise e
        finally:
            # Application.Quit()
            pass